//
//  MYPersonalCenterViewController.m
//  MiYuan_iOS
//
//  Created by zhangyongbing on 2018/4/18.
//  Copyright © 2018年 zhangyongbing. All rights reserved.
//

#import "MYPersonalCenterViewController.h"
#import "MYPersonal_HttpAPI.h"
#import "MYInformationViewController.h"
#import "MYAboutUsViewController.h"
#import "MYReplenishmentRecordViewController.h"

@interface MYPersonalCenterViewController ()
@property (nonatomic,strong) NSMutableDictionary    *dicPersonal;
@property (nonatomic,weak) IBOutlet       UILabel     *labName;
@property (nonatomic,weak) IBOutlet       UILabel     *labPhone;
@property (nonatomic,weak) IBOutlet       UILabel     *labSccount;
@property (nonatomic,weak) IBOutlet       UIButton    *btnLogout;
@property (nonatomic,weak) IBOutlet   UIView          *viewHead;

@end

@implementation MYPersonalCenterViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getPersonalInformationDataFromServices];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    CGColorRef color = [UIColor colorWSHHFromHexString:@"#FC2A1C"].CGColor;
    
    //    [self.field_biaoti setTitleColor:[UIColor colorWSHHFromHexString:@"#898989"] forState:UIControlStateNormal];
    [self.btnLogout.layer setMasksToBounds:YES];
    [self.btnLogout.layer setCornerRadius:6]; //设置矩形四个圆角半径
    [self.btnLogout.layer setBorderWidth:0.5]; //边框宽度
    [self.btnLogout.layer setBorderColor:color];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getPersonalInformationDataFromServices
{
    [MYPersonal_HttpAPI myGetPersonalInformationWithcomplationHandler:^(OCJBaseResponceModel *responseModel) {
        NSDictionary *dic = responseModel.ocjDic_data;
        [self.dicPersonal removeAllObjects];
        [self.dicPersonal addEntriesFromDictionary:dic];
        [self reloadPersonalInformation];
    }];
}

- (void)reloadPersonalInformation
{
    NSString *name = [NSString stringWithFormat:@"%@",[self.dicPersonal objectForKey:@"name"]];
    if ([name stringIsValid]) {
        [self.labName setText:name];
    }
    else{
        [self.labName setText:@""];
    }
    
    NSString *account = [NSString stringWithFormat:@"%@",[self.dicPersonal objectForKey:@"account"]];
    if ([account stringIsValid]) {
        [self.labSccount setText:[NSString stringWithFormat:@"账户：%@",account]];
    }
    else{
        [self.labSccount setText:@"账户："];
    }
    
    NSString *phone = [NSString stringWithFormat:@"%@",[self.dicPersonal objectForKey:@"phone"]];
    if ([phone stringIsValid]) {
        [self.labPhone setText:[NSString stringWithFormat:@"联系电话：%@",phone]];
    }
    else{
        [self.labPhone setText:@"联系电话："];
    }
}

- (IBAction)onLogoutButtonPressed:(id)sender
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"access_token"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [MY_NOTICE_CENTER postNotificationName:NEED_LOGIN object:nil];
}

- (IBAction)onReplenishmentRecordButtonPressde:(id)sender
{
    MYReplenishmentRecordViewController *ctrl = [[MYReplenishmentRecordViewController alloc] initWithNibName:@"MYReplenishmentRecordViewController" bundle:nil];
    [[PublicDefine shareInstance].navigation pushViewController:ctrl animated:YES];
}

- (IBAction)onInformationButtonPressde:(id)sender
{
    MYInformationViewController *ctrl = [[MYInformationViewController alloc] initWithInformationDetailDic:self.dicPersonal];
    [[PublicDefine shareInstance].navigation pushViewController:ctrl animated:YES];
}

- (IBAction)onAboutUsbuttonPressed:(id)sender
{
    MYAboutUsViewController *ctrl = [[MYAboutUsViewController alloc] initWithNibName:@"MYAboutUsViewController" bundle:nil];
    [[PublicDefine shareInstance].navigation pushViewController:ctrl animated:YES];
}

- (NSMutableDictionary *)dicPersonal
{
    if (_dicPersonal == nil) {
        _dicPersonal = [[NSMutableDictionary alloc] init];
    }
    return _dicPersonal;
}

@end
